package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {
    @Autowired
    DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
/**
 * 新增菜品接口
 * @PostMapping 表示这是一个HTTP POST请求映射
 * @ApiOperation("新增菜品") 表示接口的API文档说明为"新增菜品"
 * @param dishDTO 接收前端传来的菜品数据传输对象
 * @return 返回操作结果，成功则返回成功状态
 */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result addDish(@RequestBody  DishDTO dishDTO){
    // 打印日志，记录新增菜品的信息
        log.info("新增菜品==========================={}",dishDTO);
    // 调用服务层方法处理新增菜品逻辑
        dishService.addDish(dishDTO);
        //更新清理缓存
        redisTemplate.delete("dish_"+dishDTO.getCategoryId());
    // 返回成功结果
        return Result.success();

    }
    @GetMapping("page")
    @ApiOperation("分页查询菜品")
    public Result page(SetmealPageQueryDTO dto){
        PageResult page=dishService.getPage(dto);
        return Result.success(page);
    }
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result deleteDish(@RequestParam List<Long> ids){
        log.info("批量删除菜品==========================={}",ids);
        dishService.deleteDishBatch(ids);
        Set keys = redisTemplate.keys("*dish_*");
        redisTemplate.delete(keys);

        return Result.success();
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id获取菜品完整信息")
    public Result getDish(@PathVariable Long id){

        DishVO dishVO=dishService.getConcreateDish(id);
        return Result.success(dishVO);
    }
    @PutMapping
    @ApiOperation("修改菜品")
    public Result updateDish(@RequestBody DishDTO dishDTO){
        log.info("修改菜品==========================={}",dishDTO);
        dishService.updateDish(dishDTO);
        Set keys = redisTemplate.keys("*dish_*");
        redisTemplate.delete(keys);

        return Result.success();
    }
    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售停售")
    public Result updateDishStatus(@PathVariable Integer status,Long id){
        dishService.updateDishStatus(status,id);
        Set keys = redisTemplate.keys("*dish_*");
        redisTemplate.delete(keys);

        return Result.success();
    }
    @GetMapping("/list")
    @ApiOperation("根据菜品分类id查询菜品列表")
    public Result getDishByCateory(Long categoryId){
       List<Dish> dishes= dishService.getDishByCategoryId(categoryId);
       return Result.success(dishes);
    }

}
